# -*- coding: utf-8 -*-
import os
import ix
from liberHook.BaseHook import BaseHook
from liberLibs.dccLibs import Clarisse
from liberLibs.dccLibs.Clarisse import undo
from liberLibs.path import Path
from liberLibs.utils import copy, get_texture_abs_path


class Hook(BaseHook):
    def __init__(self, library, directory, ext, start, end):
        BaseHook.__init__(self, library, directory, ext, start, end)

    def execute(self):
        sels = ix.selection
        if sels.get_count() == 1:
            context = sels[0]
            if context.is_context():
                ix.begin_command_batch("ContextExport")
                geos = Clarisse.geometry_in_context(context)
                textures = Clarisse.texture_in_context(context)
                # export geometry
                geo_information = self.information(geos, "geo")
                for i in geo_information:
                    node = i.get("node")
                    old = str(i.get("old"))
                    new = str(i.get("new"))
                    new_path = os.path.join(os.path.dirname(new), os.path.basename(old)).replace("\\", "/")
                    copy(old, new_path)
                    ix.cmds.SetValues(["%s.filename[0]" % str(node)], [new_path])

                # export texture
                texture_information = self.information(textures, "texture")
                for i in texture_information:
                    node = i.get("node")
                    old = str(i.get("old"))
                    new = str(i.get("new"))
                    old_textures = get_texture_abs_path(old)
                    for texture in old_textures:
                        if os.path.isfile(texture):
                            new_path = os.path.join(os.path.dirname(new), os.path.basename(texture)).replace("\\", "/")
                            copy(old, new_path)
                            ix.cmds.SetValues(["%s.filename[0]" % str(node)], [new])
                ix.export_context_as_project(context, str(self.file))
                informations = geo_information + texture_information
                for i in informations:
                    i.get("node").attrs.filename = str(i.get("old"))
                ix.end_command_batch()
            else:
                self.append_error("Only context support.")
        else:
            self.append_error("Only one context support.")

    def information(self, nodes, typ):
        resolution = list()
        node_dir = os.path.join(self.directory, typ).replace("\\", "/")
        for node in nodes:
            temp_dict = dict()
            filename = node.get_attribute("filename").get_string().replace("\\", "/")
            if not filename:
                continue
            basename = Path(filename).basename()
            new_file_name = os.path.join(node_dir, basename).replace("\\", "/")
            temp_dict["node"] = node
            temp_dict["old"] = filename
            temp_dict["new"] = new_file_name
            resolution.append(temp_dict)
        return resolution
